package nl.cwi.arne.ui.jobs;

import nl.cwi.arne.Network;
import nl.cwi.arne.diagram.part.ArneDiagramEditor;
import nl.cwi.arne.ui.ArnePreferences;
import nl.cwi.arne.ui.ArneUIPlugin;
import nl.cwi.arne.ui.parts.NetworkActivationBufferWithAutoSave;
import nl.cwi.arne.util.NetworkActivationBuffer;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

/**
 * Job for generating network activations.
 * @author Christian Krause
 */
public class GenerateActivationsJob extends AbstractArneUIJob {
	
	// Diagram editor:
	private ArneDiagramEditor editor;
	
	/**
	 * Default constructor.
	 */
	public GenerateActivationsJob() {
		super("Generating activations...");
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	protected IStatus run(IProgressMonitor monitor) {
		
		// Check if the editor is there.
		if (editor==null) {
			return new Status(IStatus.ERROR, ArneUIPlugin.PLUGIN_ID, 0, "Arne editor not set", null);
		}
		
		// Get the network and create an activation buffer:
		Network network = editor.getNetwork();
		NetworkActivationBuffer buffer = ArnePreferences.isAutoSave() ?
										new NetworkActivationBufferWithAutoSave(editor) :
										new NetworkActivationBuffer(network);
		buffer.setEditingDomain(editor.getEditingDomain());
		
		// Generate the activations:
		getInitializedGenerator().generate(network, buffer, monitor);
		
		// Done.
		return Status.OK_STATUS;
		
	}
	
	public void setEditor(ArneDiagramEditor editor) {
		this.editor = editor;
	}
	
}
